﻿
(function () {
    appModule.controller('common.views.cameraManages.createOrEditModal', [
        '$scope', '$uibModalInstance', 'abp.services.app.cameraManage', 'cameraManageId', 'abp.services.app.area', 'cameraManage',
        function ($scope, $uibModalInstance, cameraManageService, cameraManageId, areaService, cameraManage) {
            var vm = this;
            vm.saving = false;
            //首先将cameraManage数据设置为null
            vm.cameraManage = null;



            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                cameraManageService.createOrUpdateCameraManageAsync({ cameraManageEditDto: vm.cameraManage }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });


            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定镇控件
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                });

                if (cameraManageId != null) {
                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: cameraManage.townID }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: cameraManage.areaID }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        cameraManageService.getCameraManageForEditAsync({
                            id: cameraManageId
                        }).then(function (result) {
                            vm.cameraManage = result.data.cameraManage;
                        });
                    });
                }

            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();